import { Global, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigurationService } from './services/configration.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { UserService } from './services/user.service';
import { IsUsernameUniqueConstraint } from './validators/user-validator';
import { UtilityService } from './services/utility.service';

@Global()
@Module({
  providers: [
    ConfigurationService,
    UserService,
    IsUsernameUniqueConstraint,
    UtilityService,
  ],
  exports: [
    ConfigurationService,
    UserService,
    IsUsernameUniqueConstraint,
    UtilityService,
  ],
  imports: [
    ConfigModule.forRoot({
      isGlobal: true, // 默认加载.env文件
    }),
    TypeOrmModule.forRootAsync({
      inject: [ConfigurationService],
      useFactory: (configService: ConfigurationService) => ({
        type: 'mysql',
        ...configService.mysqlConfig,
        autoLoadEntities: true, // 自动加载实体
        synchronize: true, // 保持代码和数据库的一致
        logging: false, // 打印内部真正的SQL语句
        // entities: [User]
      }),
    }),
    TypeOrmModule.forFeature([User]),
  ],
})
export class SharedModule {}
